﻿using ActionSystem.Core.HitLogic;
using ActionSystem.Core.Ranges;
using ActionSystem.Core.Utils;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace ActionSystem.Core.HitLogic
{
    [PoolInitAttribute(typeof(GameObjectPool<BoxRangeBoxItem>), 50)]
    public class BoxRangeBoxItem : RangeBoxItem
    {
        public override Collider InitCollider()
        {
            this.GetType().GetCustomAttributes(true);
            return gameObject.AddComponent<BoxCollider>();
        }

        protected override void PutBackToPool()
        {
            if(!IsDeleted)
            {
                GameObjectPool<BoxRangeBoxItem>.Delete(this);
            }            
        }

        public override void SetRangeShape(IItem item)
        {
            base.SetRangeShape(item);
            BoxItem r = item as BoxItem;
            if (r != null)
            {
                var box = hitBox as BoxCollider;
                if(box != null)
                {
                    box.transform.rotation = Quaternion.Euler(r.rotation);                   
                    box.transform.position = new Vector3(r.offset.x, r.offset.y, r.offset.z);
                    box.center = Vector3.zero;
                    box.size = r.size;
                }
            }
        }
        public override int HitDetect(int unityLayerMask)
        {
            if (value == null||unityLayerMask==0)
            {
                return 0;
            }
            BoxItem box = value as BoxItem;
            if (box == null)
            {
                return 0;
            }
#if UNITY_EDITOR
            var defaultColor = XMLib.DrawUtility.D.color;
            XMLib.DrawUtility.D.color = Color.yellow;
            XMLib.DrawUtility.D.duration = 1 / 60f;
            var m1 = transform.localToWorldMatrix;
            XMLib.DrawUtility.D.DrawBox(box.size, m1);
            XMLib.DrawUtility.D.color = defaultColor;
#endif
            return Physics.OverlapBoxNonAlloc(transform.position, box.size / 2, HitResult, transform.rotation, unityLayerMask);
        }
    }
}


